<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//

//
if( $action == 'doregister' ) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.validate.class.php');
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'account.validate.class.php');
	$vObj = new AccountValidate();
	$vObj->checkRegister($_POST);
	
	//
	if( $vObj->_valid() ) {
		if( isset($_POST['field_username']) && $userObj->checkUsername($_POST['field_username']) ) {
			$vObj->_add('field_username', 'The username already exists');
		}
		if( isset($_POST['field_email_address']) && $userObj->checkUserEmail($_POST['field_email_address']) ) {
			$vObj->_add('field_email_address', 'The email address already exists');
		}
	}
	
	//
	if( $vObj->_valid() == false ) {
		$GLOBALS['_SUBMITTED_'] = 1;
		
		$vm = $vObj->_result(':error');
		$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		
		$vr = $vObj->_valid_result();
		$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vr', $vr);
	} else {
		$data_array = array(
					'users_name' => (isset($_POST['field_username']) ? $_POST['field_username'] : ''),
					'users_email_address' => $_POST['field_email_address'],
					'users_password' => $_POST['field_password'],
					'users_contactname' => (isset($_POST['field_contactname']) ? $_POST['field_contactname'] : $_POST['field_firstname'] . ' ' . $_POST['field_lastname']),
					'users_firstname' => $_POST['field_firstname'],
					'users_lastname' => $_POST['field_lastname'],
					'users_company' => $_POST['field_company'],
					//
					'users_description' => (isset($_POST['field_message']) ? $_POST['field_message'] : ''),
						);
		//
		if( CONST_USER_FIELDS_ENABLED == '1' && CONST_USER_FIELDS_COUNT > 0 ) {
			for($i=1; $i<=CONST_USER_FIELDS_COUNT; $i++) {
				if( isset($_POST['field_field' . $i]) ) {
					$data_array['users_field' . $i] = $_POST['field_field' . $i];
				}
			}
		}
		//
		$users_id = $userObj->addUserF($data_array, 'customer');
		if( $users_id > 0 ) {
			$data_array = array(
						'email_address' => $_POST['field_email_address'],
						'password' => $_POST['field_password'],
							);
			$result = $userObj->doLogin($data_array);
			
			//notify
			$content = array(
						'user' => array(
								'users_username' => (isset($_POST['field_username']) ? $_POST['field_username'] : ''),
								'users_firstname' => $_POST['field_firstname'],
								'users_lastname' => $_POST['field_lastname'],
								'users_contactname' => (isset($_POST['field_contactname']) ? $_POST['field_contactname'] : $_POST['field_firstname'] . ' ' . $_POST['field_lastname']),
								'users_email_address' => $_POST['field_email_address'],
								'users_password' => $_POST['field_password'],
									),
							);
			
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.mail.class.php');
			$umnObj = new UserMailNotify();
			$umnObj->setMailTo($content['user']['users_email_address'], $content['user']['users_firstname']);
			$umnObj->addContent($content);
			$umnObj->bindRegisterContent();
			$umnObj->setMailContent(':register');
			
			$umnObj->sendmail();
			
			//cron
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.cron.class.php');
			$cronObj = new CoreCronBase();
			$cronObj->updateNextRun('mail_queue', 'link');
			
			//
			util_redirect('member/');
		}
	}
}

//link
if( CONST_ACCOUNT_NEW_TYPE == 'apply' ) {
	if( $request_link_class == 'success' ) {
		$content_wrap = array(
					'mainpart' => 'apply_success',
						);
	} else {
		$content_wrap = array(
					'mainpart' => 'apply',
					'user' => $userObj->getFormatUser(),
						);
	}
} else {
	if( $request_link_class == 'success' ) {
		$content_wrap = array(
					'mainpart' => 'register_success',
						);
	} else {
		$content_wrap = array(
					'mainpart' => 'register',
					'user' => $userObj->getFormatUser(),
						);
	}
}

//
$feObj->content_wrap = $content_wrap;
echo $feObj->display('pagelayout/pagelayout_member_register.tpl');
//
?>